<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="zh-cn" xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="description" content="DnsMasqWeb">
		<title>域名管理 - DnsMasqWeb</title>
         {% load static %}
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<link href="/static/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
		<link href="/static/css/dnsmasqweb.css" rel="stylesheet">
	</head>
	<body>
		<div class="container">
			{% include "top_nav.html" %}
			<div class="row">
				{% include "nav.html" %}
				<div class="span10">
					<h4>
						域名管理
					</h4>
                    <div class="pull-right">
                        <input type="button" class="btn btn-primary" value="添加域名" onclick="show_add();">
                    </div><br /><br />
					<table class="table table-bordered table-striped table-hover table-condensed">
						<thead>
							<tr>
								<th><input type="checkbox" id="checkbox_all"></th>
                                <th>域名</th>
								<th title="MD5：b21a3666b180025da9ea2467b4dffba2">域名文件</th>
								<th>创建时间</th>
								<th>更新时间</th>
								<th>备注</th>
                                <th>操作</th>
							</tr>
						</thead>
						<tbody>
                            <tr id="add_line" class="display_no">
								<td></td>
                                <td><input type="text" name="domain" id="domain"  class="input-medium" onkeyup="set_file_value('domain','file');"></td>
                                <td><input type="text" name="file" id="file"  class="input-medium" onblur="set_file_value('domain','file');"></td>
                                <td>{{get_time}}</td>
                                <td>{{get_time}}</td>
                                <td><input type="text" name="comment" id="comment"  class="input-medium"></td>
								<td>
                                    <a href="javascript:void(0);" class="view-link" onclick="add_domain();">保存</a> |
                                    <a href="javascript:void(0);" class="view-link" onclick="cancel_add();">取消</a>
                                </td>
							</tr>
                            {% load set_var %}
                            {% for d in domains %}
                            {% if d.status == 'yes' %}
                            <tr id="{{d.id}}">
                            {% set st_msg = "暂停" %}
                            {% set fun_value = "no" %}
                            {% else %}
							<tr id="{{d.id}}" class="error">
                            {% set st_msg = "开启" %}
                            {% set fun_value = "yes" %}
                            {% endif %}
								<td><input type="checkbox"/> {{ loop.index }}</td>
                                <td><a href="/dns/record/?did={{d.id}}" title="查看 {{d.domain}} 的DNS记录">{{ d.domain }}</a></td>
                                <td title="MD5：{{ d.file_md5 }}">{{ d.file }}</td>
                                <td>{{ d.create_time }}</td>
                                <td>{{ d.update_time }}</td>
                                <td>{{ d.comment }}</td>
                                <td>
                                    <a href="javascript:void(0);" onclick="to_edit({{d.id}})">编辑</a> |
                                    <a href="/public/api?module=domain&fun=ch_status&id={{d.id}}&value={{fun_value}}" onclick="return confirm('确认{{st_msg}}该域名？');">{{st_msg}}</a> |
                                    <a href="/public/api?module=domain&fun=del&id={{d.id}}" onclick="return confirm('确认删除该域名，并且同时删除该域名所有记录？');">删除</a>
                                </td>
							</tr>
                            {# 可编辑状态 #}
                            <tr id="edit_line_{{d.id}}" class="display_no">
                                <td><input type="checkbox"> {{loop.index}}</td>
                                <td><input type="text" class="input-medium" value="{{d.domain}}" name="domain_{{d.id}}" id="domain_{{d.id}}" onkeyup="set_file_value('domain_'+{{d.id}},'file_'+{{d.id}});"></td>
                                <td><input type="text" class="input-medium" value="{{d.file}}" name="file_{{d.id}}" id="file_{{d.id}}"></td>
                                <td>{{d.create_time}}</td>
                                <td>{{d.update_time}}</td>
                                <td><input type="text" class="input-medium" value="{{d.comment }}" name="comment_{{d.id}}" id="comment_{{d.id}}"></td>
                                <td>
                                    <a href="javascript:void(0);" onclick="save_info({{d.id}});">保存</a> |
                                    <a href="javascript:void(0);" onclick="to_line({{d.id}})">取消</a>
                                </td>
                            </tr>
                            {% endfor %}
						</tbody>
					</table>
					<ul class="pager">
						<li class="next">
							<a href="more">More &rarr;</a>
						</li>
					</ul>
				</div>
			</div>
		</div>
		<script src="/static/js/jquery-1.8.3.min.js"></script>
		<script src="/static/bootstrap/js/bootstrap.min.js"></script>
		<script src="/static/js/dnsmasqweb.js"></script>
        <script type="text/javascript">
            function add_domain() {
                domain = $("#domain").val();
                file = $("#file").val();
                comment = $("#comment").val();
                if ( domain == '') {
                    alert("请输入域名！");
                    $("#domain").focus();
                    return false;
                };
                if ( file == '') {
                    alert("请输入域名文件！");
                    $("#file").focus();
                    return false;
                };
                $.ajax({
                    type: "POST",
                    url: "/dns/domain/",
                    data: { 'domain':domain,'file':file, 'comment':comment },
                    dataType: "text",
                    success: function(msg){
                        if (msg == "1") {
                            alert("添加成功！");
                            window.location.href="/dns/domain";
                        } else if (msg == "2") {
                            alert("域名已存在！");
                            $("#domain").focus();
                        } else {
                            alert("提示：添加域名失败！");
                        }
                        },
                    error:function(){
                        alert("提示：服务器内部错误！");
                        },
                    });
            };
            function show_add(){
                $("#add_line").removeClass("display_no");
                $("#domain").focus();
            };
            function cancel_add(){
                $("#add_line").addClass("display_no");
            };
            /* 同步动态输入域名配置文件 */
            function set_file_value(id1,id2){
                d = $("#"+id1).val();
                if ( d != '' ) {
                    $("#"+id2).val(d + ".conf");
                } else {
                    $("#"+id2).val('');
                };
            };
            function to_edit(id){
                show_id = "#line_" + id;
                edit_id = "#edit_line_" + id;
                $(show_id).addClass("display_no");
                $(edit_id).removeClass("display_no");
            };
            function to_line(id){
                show_id = "#line_" + id;
                edit_id = "#edit_line_" + id;
                $(show_id).removeClass("display_no");
                $(edit_id).addClass("display_no");
            };
            /* 编辑保存记录 */
            function save_info(id){
                domain = $("#domain_"+id).val();
                file = $("#file_"+id).val();
                comment = $("#comment_"+id).val();
                $.ajax({
                    type: "POST",
                    url: "/dns/domain/",
                    data: { "id":id, "domain":domain, "file":file, "comment":comment, "fun":"edit" },
                    dataType: "text",
                    success: function(msg){
                        if (msg == "1") {
                            alert("修改成功！");
                            location.href="/dns/domain/";
                        } else {
                            alert("提示：修改失败！");
                            return false;
                        }
                        },
                    error:function(){
                        alert("提示：服务器内部错误！");
                        return false;
                        },
                    });
            };
        </script>

	</body>
</html>